package com.sy.simpleegg.util

import android.util.Log
import com.sy.simpleegg.constant.BanknoteChannelConfig
import com.sy.simpleegg.constant.Country
import com.sy.simpleegg.constant.CountryParam
import com.sy.simpleegg.constant.CurrencyUnit
import com.sy.simpleegg.ext.logFile
import com.sy.simpleegg.platform.BackstagePlatform
import org.cnbleu.serialport.SerialPort
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import java.util.Locale
import java.util.UUID

object CommonUtil {
    fun getUuid(): String{
        return UUID.randomUUID().toString()
    }

    fun getBaudrate(b: String): SerialPort.BAUDRATE{
        return when (b){
            SerialPort.BAUDRATE.B0.name -> SerialPort.BAUDRATE.B0
            SerialPort.BAUDRATE.B50.name -> SerialPort.BAUDRATE.B50
            SerialPort.BAUDRATE.B75.name -> SerialPort.BAUDRATE.B75
            SerialPort.BAUDRATE.B110.name -> SerialPort.BAUDRATE.B110
            SerialPort.BAUDRATE.B134.name -> SerialPort.BAUDRATE.B134
            SerialPort.BAUDRATE.B150.name -> SerialPort.BAUDRATE.B150
            SerialPort.BAUDRATE.B200.name -> SerialPort.BAUDRATE.B200
            SerialPort.BAUDRATE.B300.name -> SerialPort.BAUDRATE.B300
            SerialPort.BAUDRATE.B600.name -> SerialPort.BAUDRATE.B600
            SerialPort.BAUDRATE.B1200.name -> SerialPort.BAUDRATE.B1200
            SerialPort.BAUDRATE.B1800.name -> SerialPort.BAUDRATE.B1800
            SerialPort.BAUDRATE.B2400.name -> SerialPort.BAUDRATE.B2400
            SerialPort.BAUDRATE.B4800.name -> SerialPort.BAUDRATE.B4800
            SerialPort.BAUDRATE.B9600.name -> SerialPort.BAUDRATE.B9600
            SerialPort.BAUDRATE.B19200.name -> SerialPort.BAUDRATE.B19200
            SerialPort.BAUDRATE.B38400.name -> SerialPort.BAUDRATE.B38400
            SerialPort.BAUDRATE.B57600.name -> SerialPort.BAUDRATE.B57600
            SerialPort.BAUDRATE.B115200.name -> SerialPort.BAUDRATE.B115200
            SerialPort.BAUDRATE.B230400.name -> SerialPort.BAUDRATE.B230400
            SerialPort.BAUDRATE.B460800.name -> SerialPort.BAUDRATE.B460800
            SerialPort.BAUDRATE.B500000.name -> SerialPort.BAUDRATE.B500000
            SerialPort.BAUDRATE.B576000.name -> SerialPort.BAUDRATE.B576000
            SerialPort.BAUDRATE.B921600.name -> SerialPort.BAUDRATE.B921600
            SerialPort.BAUDRATE.B1000000.name -> SerialPort.BAUDRATE.B1000000
            SerialPort.BAUDRATE.B1152000.name -> SerialPort.BAUDRATE.B1152000
            SerialPort.BAUDRATE.B1500000.name -> SerialPort.BAUDRATE.B1500000
            SerialPort.BAUDRATE.B2000000.name -> SerialPort.BAUDRATE.B2000000
            SerialPort.BAUDRATE.B2500000.name -> SerialPort.BAUDRATE.B2500000
            SerialPort.BAUDRATE.B3000000.name -> SerialPort.BAUDRATE.B3000000
            SerialPort.BAUDRATE.B3500000.name -> SerialPort.BAUDRATE.B3500000
            else -> SerialPort.BAUDRATE.B9600
        }
    }

    fun getStopb(s: String): SerialPort.STOPB{
        return when (s){
            SerialPort.STOPB.B1.name -> SerialPort.STOPB.B1
            SerialPort.STOPB.B2.name -> SerialPort.STOPB.B2
            else -> SerialPort.STOPB.B1
        }
    }

    fun getDatab(d: String): SerialPort.DATAB{
        return when (d){
            SerialPort.DATAB.CS5.name -> SerialPort.DATAB.CS5
            SerialPort.DATAB.CS6.name -> SerialPort.DATAB.CS6
            SerialPort.DATAB.CS7.name -> SerialPort.DATAB.CS7
            SerialPort.DATAB.CS8.name -> SerialPort.DATAB.CS8
            else -> SerialPort.DATAB.CS5
        }
    }

    fun getParity(p: String): SerialPort.PARITY{
        return when (p){
            SerialPort.PARITY.NONE.name -> SerialPort.PARITY.NONE
            SerialPort.PARITY.ODD.name -> SerialPort.PARITY.ODD
            SerialPort.PARITY.EVEN.name -> SerialPort.PARITY.EVEN
            else -> SerialPort.PARITY.NONE
        }
    }

    /**
     * 生成一个六位不重复的字符
     */
    fun unRepeatSixCode(): String {
        return try {
            var sixChar = ""
            val sdf = SimpleDateFormat("yyMMddHHmmss")
            val date = Date()
            val time = sdf.format(date)
            for (i in 0 until time.length / 2) {
                var singleChar: String
                val x = time.substring(i * 2, (i + 1) * 2)
                val b = x.toInt()
                singleChar = if (b < 10) {
                    Integer.toHexString(x.toInt())
                } else if (b >= 10 && b < 36) {
                    (x.toInt() + 55).toChar().toString()
                } else {
                    (x.toInt() + 61).toChar().toString()
                }
                sixChar = sixChar + singleChar
            }
            Log.d(javaClass.simpleName, "生成一个6位不可重复的字符编码是：$sixChar")
            sixChar
        }catch (e: Exception){
            e.printStackTrace()
            "aaaaaa"
        }
    }

    /**
     * 获取接入后台类型
     * @param backstageCode 平台代号
     */
    fun getBackstage(backstageCode: Int): BackstagePlatform{
        BackstagePlatform.entries.forEach { bs ->
            if (bs.backstageCode == backstageCode){
                return bs
            }
        }
        return BackstagePlatform.DEF_MQTT
    }

    /**
     * 匹配国家货币符号
     * @param countryCode 国家，地区 代码
     */
    fun getCurrencySymbols(countryCode: Int): String{
        var shortName = ""
        Country.entries.forEach { c ->
            if (c.countryCode == countryCode){
                shortName = c.shortName
            }
        }
        CountryParam.entries.forEach { cp ->
            if (cp.shortName == shortName){
                return cp.moneyChar
            }
        }
        return CountryParam.CN.moneyChar
    }

    /**
     * 匹配国家语言
     * @param countryCode 国家，地区 代码
     */
    fun getLanguage(countryCode: Int): String{
        var shortName = ""
        Country.entries.forEach { c ->
            if (c.countryCode == countryCode){
                shortName = c.shortName
            }
        }
        CountryParam.entries.forEach { cp ->
            if (cp.shortName == shortName){
                return "${cp.lan},${cp.area}"
            }
        }
        return ""
    }

    /**
     * 获取货币单位
     * @param countryCode 国家，地区 代码
     */
    fun getCurrencyUnit(countryCode: Int): Int{
        var shortName = "CN"
        Country.entries.forEach { c ->
            if (c.countryCode == countryCode){
                shortName = c.shortName
            }
        }
        val currencyUnitList: MutableList<CurrencyUnit> = mutableListOf()
        CurrencyUnit.entries.forEach { cu ->
            if (cu.countryShortName == shortName){
                currencyUnitList.add(cu)
            }
        }
        if (currencyUnitList.isEmpty()){
            return CurrencyUnit.COMMON.currencyCode
        }else{
            currencyUnitList.sortBy { it.sort }
            return currencyUnitList[0].currencyCode
        }
    }

    /**
     * 获取纸钞通道模板
     * @param countryCode 国家（地区）代码
     */
    fun getChannelMode(countryCode: Int): String{
        var shortName = "CN"
        Country.entries.forEach { c ->
            if (c.countryCode == countryCode){
                shortName = c.shortName
            }
        }
        var channelParam = BanknoteChannelConfig.CN.channelConfig + ";" + BanknoteChannelConfig.CN.area
        BanknoteChannelConfig.entries.forEach { channelConfig ->
            if (channelConfig.area == shortName){
                channelParam = channelConfig.channelConfig + ";" + channelConfig.area
            }
        }
        return channelParam
    }

    /**
     * 获取纸钞通道模板
     * @param area 国家（地区）简称
     */
    fun getChannelModeByArea(area: String): String{
        var channelParam = BanknoteChannelConfig.CN.channelConfig + ";" + BanknoteChannelConfig.CN.area

        if (area.isEmpty()) return channelParam

        BanknoteChannelConfig.entries.forEach { channelConfig ->
            if (channelConfig.area == area){
                channelParam = channelConfig.channelConfig + ";" + channelConfig.area
            }
        }
        return channelParam
    }

    /**
     * 获取过去x天的日期
     * @param sum 天数
     * @param pattern 日期格式 默认：yyyy-MM-dd
     */
    fun getPassDate(sum: Int, pattern: String = "yyyy-MM-dd"): Array<String> {
        return try {
            val passDates: MutableList<String> = mutableListOf()
            val calendar: Calendar = Calendar.getInstance()

            // 设置时区，根据需要设置
//            calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"))

            val sdf = SimpleDateFormat(pattern, Locale.getDefault())
            // 循环获取前sum天的日期
            for (i in 0 until sum) {
                // 格式化日期
                val formatDate = sdf.format(calendar.time)
                passDates.add(formatDate)

                // 将日历回退一天
                calendar.add(Calendar.DATE, -1)
            }
            passDates.toTypedArray()
        }catch (e: Exception){
            e.printStackTrace()
            logFile("获取过去x天日期异常：${e.message}")
            arrayOf()
        }
    }
}